<?php

class SettingsController extends Controller
{

    public $layout = '//layouts/column1';

    public function actionIndex()
    {
        $this->render('index');
    }

    public function actionListCategory()
    {
        $criteria = new CDbCriteria();

        $dataProvider = new CActiveDataProvider('Category', array(
                    'criteria' => $criteria
                ));

        $this->render('listCategories', array(
            'dataProvider' => $dataProvider
        ));
    }

    public function actionCreateCategory()
    {
        $model = new Category();

        if (isset($_POST['Category'])) {
            $model->setAttributes($_POST['Category']);

            if ($model->validate() && $model->save()) {
                Yii::app()->user->setFlash('success', 'You have insert new category successful');
            } else {
                Yii::app()->user->setFlash('error', 'An Error occurs!');
            }
        }

        $this->render('formCategory', array(
            'model' => $model
        ));
    }

    public function actionQuantity()
    {
        $model = new Settings();
        if (isset($_POST['Settings'])) {
            $data = $_POST['Settings'];
            $status = array();
            foreach ($data as $key => $value) {
                $m = Settings::model()->findByPk($key);
                if ($m == null) {
                    $m = new Settings;
                }
                $m->key = $key;
                $m->value = $value['value'];
                if ($m->validate() && $m->save()) {
                    $status[] = 1;
                } else {
                    $status[] = 0;
                }
            }
        }
        $this->render('quantity', array(
            'model' => $model
        ));
    }

}
